﻿Imports DTO
Imports BUS
Imports DevExpress.XtraEditors

Public Class frmThemKhoHang

    Public Delegate Sub ThemKhoH(kh As KhoHangDTO)

    Public Event LamMoiKhoHKhiThem As ThemKhoH

    Public kt As Integer = 0

    Public makh As String
    Public tenkh As String
    Public nlh As String
    Public dt As String
    Public dc As String
    Public kh As String
    Public dg As String
    Public matt As Integer
    Public nql As String


    Private Sub LoadCbxNhanVien()
        cbxThemNQLKhoHang.Properties.DataSource = NhanVienBUS.LayDSNhanVienCBX()
        cbxThemNQLKhoHang.Properties.ValueMember = "MaNhanVien"
        cbxThemNQLKhoHang.Properties.DisplayMember = "TenNhanVien"
    End Sub

    Sub ThemKhoHang()
        KhoHangBUS.DongKetNoi.Close()
        Dim khohang As New KhoHangDTO

        khohang.KyHieu = txtThemKyHieuKhoHang.Text.Trim()
        khohang.TenKhoHang = txtThemTenKhoHang.Text.Trim()
        If cbxThemNQLKhoHang.Text.Trim() = "" Then
            khohang.MaNguoiQL = Nothing
        Else
            khohang.MaNguoiQL = cbxThemNQLKhoHang.EditValue.ToString()
        End If
        khohang.NguoiLienHe = txtThemNLHKhoHang.Text.Trim()
        khohang.DiaChi = txtThemDiaChiKhoHang.Text.Trim()
        khohang.DienThoai = txtThemDienThoaiKhoHang.Text.Trim()
        khohang.Fax = txtThemFaxKhoHang.Text.Trim()
        khohang.Email = txtThemEmailKhoHang.Text.Trim()
        khohang.DienGia = txtThemDienGiaiKhoHang.Text.Trim()
        If chkThemQuanLyKhoHang.Checked = True Then
            khohang.MaTrangThai = 1
        Else
            khohang.MaTrangThai = 0
        End If

        If KhoHangBUS.ThemKhoHang(khohang) Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Thêm thành công kho hàng", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Tên kho hàng không được để trống", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        RaiseEvent LamMoiKhoHKhiThem(khohang)
    End Sub

    Sub CapNhatKhoHang()
        KhoHangBUS.DongKetNoi.Close()
        Dim khohang As New KhoHangDTO

        khohang.MaKhoHang = txtThemMaKhoHang.Text.Trim()
        khohang.KyHieu = txtThemKyHieuKhoHang.Text.Trim()
        khohang.TenKhoHang = txtThemTenKhoHang.Text.Trim()
        If cbxThemNQLKhoHang.Text.Trim() = "" Then
            khohang.MaNguoiQL = Nothing
        Else
            khohang.MaNguoiQL = cbxThemNQLKhoHang.EditValue.ToString()
        End If
        khohang.NguoiLienHe = txtThemNLHKhoHang.Text.Trim()
        khohang.DiaChi = txtThemDiaChiKhoHang.Text.Trim()
        khohang.DienThoai = txtThemDienThoaiKhoHang.Text.Trim()
        khohang.Fax = txtThemFaxKhoHang.Text.Trim()
        khohang.Email = txtThemEmailKhoHang.Text.Trim()
        khohang.DienGia = txtThemDienGiaiKhoHang.Text.Trim()
        If chkThemQuanLyKhoHang.Checked = True Then
            khohang.MaTrangThai = 1
        Else
            khohang.MaTrangThai = 0
        End If

        If KhoHangBUS.CapNhatKhoHang(khohang) Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Cập nhật thành công kho hàng", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Tên kho hàng không được để trống", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub frmThemKH_btnThemKhoHang_Click(sender As Object, e As EventArgs) Handles frmThemKH_btnThemKhoHang.Click
        NhanVienBUS.DongKetNoi.Close()
        If kt = 1 Then
            Dim pq As PhanQuyenNhonNDDTO = ChucNangBUS.layQuyenCuaND(frmMain.maND, 20)
            If pq.Sua = True Then
                CapNhatKhoHang()
                HeThongBUS.themNhatKy(frmMain.maND, "Quản lý kho hàng", "Cập nhật", makh)
            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Xin lỗi bạn không có quyền này", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Else
            Dim pq As PhanQuyenNhonNDDTO = ChucNangBUS.layQuyenCuaND(frmMain.maND, 20)
            If pq.Them = True Then
                ThemKhoHang()
                HeThongBUS.themNhatKy(frmMain.maND, "Quản lý kho hàng", "Thêm", makh)
            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Xin lỗi bạn không có quyền này", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If
    End Sub

    Private Sub frmThemKH_btnDongKhoHang_Click(sender As Object, e As EventArgs) Handles frmThemKH_btnDongKhoHang.Click
        KhoHangBUS.DongKetNoi.Close()
        Me.Close()

    End Sub

    Private Sub frmThemKhoHang_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadCbxNhanVien()
        If kt = 1 Then
            txtThemMaKhoHang.Text = makh
            txtThemTenKhoHang.Text = tenkh
            txtThemDiaChiKhoHang.Text = dc
            txtThemDienGiaiKhoHang.Text = dg
            txtThemDienThoaiKhoHang.Text = dt
            txtThemKyHieuKhoHang.Text = kh
            txtThemNLHKhoHang.Text = nlh
            If matt = 1 Then
                chkThemQuanLyKhoHang.Checked = True
            Else
                chkThemQuanLyKhoHang.Checked = False
            End If
            KhoHangBUS.LayMaKhoHangCbx(makh, nql)
            cbxThemNQLKhoHang.EditValue = nql
        End If
    End Sub
End Class